<?php

namespace app\common\library;
use app\common\constant\Common;

class MobileMsg 
{

    
    public function sendMessage($msg,$mobile)
    {

        $ip="http://49.234.36.16:8080";

        $appid="47E96708-A892-FBE1-375D-BC84A0D78BAB";
    
        $appsecret="C6CC6018-670E-CBA0-0A9D-8FE80E6AE1B8";

        $userid='00000500217';//合作发规处

         //步骤1：获取token
     
        $token=$this->getToken(($ip,$appid,$appsecret);
    
        $post_data = array(
            "appid"         => $appid,
            "appsecret"     => $appsecret,
            "fromuser"=>$userid,
            "content"=>$msg,
            "type"=>"text",
            "priority"=>1,
            "tools"=>array(
                "tool"=>'mobile'
            )
            "tousers"=>array(
                "touser"=>$mobile
            )
    
        ); 

      
     
    
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL            => $ip."/api/send?tokenid=".$token."&schoolcode=1",
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_POST           => 1,
            CURLOPT_POSTFIELDS     => $post_data,
        ));
        $output = curl_exec($ch);
        curl_close($ch);
    
        $res=json_encode($output);
        return $res['retmsg'];
    


    
}

public function getToken($ip,$appid,$appsecret)
{
    


    $post_data = array(
        "appid"         => $appid,
        "appsecret"     => $appsecret,

    ); 

    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL            => $ip."/api/token?schoolcode=1",
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST           => 1,
        CURLOPT_POSTFIELDS     => $post_data,
    ));
    $output = curl_exec($ch);
    curl_close($ch);

    $res=json_encode($output);
    return $res['access_token'];

}
}